﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Net;
using System.Net.Sockets;
using uPLibrary.Networking.M2Mqtt;
using System.Text.RegularExpressions;
using System.Text;

namespace HelloWeb
{
    /// <summary>
    /// HelloWS 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class HelloWS : System.Web.Services.WebService
    {

        public string mqtt_server = "119.91.90.201";
        public int mqtt_port = 1883;
        public string username = "guest";
        public string password = "guest";
        public string clientId = Guid.NewGuid().ToString();

        static UInt16[] dataGreeOff = {
                8950, 4450,
                650, 550, 650, 1600, 700, 500, 700, 500, 650, 1650, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 500, 700, 500, 650, 1650, 650, 550, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 1650, 650, 550, 650, 550,
                650, 500, 700, 500, 700, 500, 650, 550, 650, 1650, 650, 550, 650, 1600, 700, 500,
                650, 550, 650, 1650, 650, 550,
                650, 19750,
                700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 1600, 650, 550,
                650, 39600, 8900, 4450,
                650, 550, 650, 1600, 700, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 1650, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 500, 700, 500, 650, 1650, 650, 1650, 650, 1600, 700, 500,
                650, 550, 650, 1650, 650, 550,
                650, 19750,
                700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550,
                650, 550, 650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 1650, 650, 550,
                650 };

        static UInt16[] dataGreeOn = { 8900, 4450, 650, 550, 650, 1600, 700, 550, 650, 1600, 650, 1650, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 1600, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 1650, 650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 550, 650, 1650, 650, 550, 650, 1600, 700, 500, 700, 500, 650, 1650, 650, 550, 650, 19750, 700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 1600, 650, 1650, 650, 39600, 8900, 4400, 700, 500, 700, 1600, 650, 550, 650, 1650, 650, 1600, 700, 550, 650, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 550, 650, 550, 650, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 1600, 650, 1650, 650, 550, 650, 550, 650, 1600, 650, 550, 650, 19800, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 700, 1600, 650, 1650, 650 };


        static MqttClient ConnectMQTT(string broker, int port, string clientId, string username, string password)
        {
            MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
            client.Connect(clientId, username, password);
            if (client.IsConnected)
            {
                Console.WriteLine("Connected to MQTT Broker");
            }
            else
            {
                Console.WriteLine("Failed to connect");
            }
            return client;
        }


        void sendData(MqttClient client, string topic, UInt16[] data)
        {
            byte[] buf = new byte[data.Length * 2]; // { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};

            for (int i = 0; i < data.Length; i++)
            {
                BitConverter.GetBytes(data[i]).CopyTo(buf, i * 2);
            }

            client.Publish(topic, buf);

        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        //[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
        public string searchPhone(string number)
        {
            //number = number.Substring(0, 7);
            //HelloWeb.PhonetTableAdapters.PhonetmpTableAdapter adapter = new PhonetTableAdapters.PhonetmpTableAdapter();
            //var row = adapter.GetDataByNumber(number).First();
            //return $"{row.Province}{row.Area}, {row.Provider}";
            return "";
        }

        [WebMethod]
        public void poweroffGreeAC(string topic = "hw/home/ircmd")
        {

            // 如果 broker 需要鉴权，设置用户名密码

            MqttClient client = ConnectMQTT(mqtt_server, mqtt_port, clientId, username, password);
            //Subscribe(client, topic);
            sendData(client, topic, dataGreeOff);
        }

        [WebMethod]
        public void WakeupPC(string mac, string topic = "hw/home/wol")
        {
            byte[] message = new byte[6];

            string[] chars = mac.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (chars.Length != 6)
                throw new FormatException();

            //逐个字符变为16进制字节数据
            for (int i = 0; i < 6; i++)
            {
                message[i] = Convert.ToByte(chars[i], 16);
            }

            //string topic = ;

            MqttClient client = ConnectMQTT(mqtt_server, mqtt_port, clientId, username, password);
            //Subscribe(client, topic);
            client.Publish(topic, message);
        }

    }

}

